var _excluded = ['value', 'onChange', 'options', 'className'];
function _extends() {
  _extends = Object.assign
    ? Object.assign.bind()
    : function (target) {
        for (var i = 1; i < arguments.length; i++) {
          var source = arguments[i];
          for (var key in source) {
            if (Object.prototype.hasOwnProperty.call(source, key)) {
              target[key] = source[key];
            }
          }
        }
        return target;
      };
  return _extends.apply(this, arguments);
}
function _toConsumableArray(arr) {
  return (
    _arrayWithoutHoles(arr) ||
    _iterableToArray(arr) ||
    _unsupportedIterableToArray(arr) ||
    _nonIterableSpread()
  );
}
function _nonIterableSpread() {
  throw new TypeError(
    'Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.',
  );
}
function _unsupportedIterableToArray(o, minLen) {
  if (!o) return;
  if (typeof o === 'string') return _arrayLikeToArray(o, minLen);
  var n = Object.prototype.toString.call(o).slice(8, -1);
  if (n === 'Object' && o.constructor) n = o.constructor.name;
  if (n === 'Map' || n === 'Set') return Array.from(o);
  if (n === 'Arguments' || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))
    return _arrayLikeToArray(o, minLen);
}
function _iterableToArray(iter) {
  if (
    (typeof Symbol !== 'undefined' && iter[Symbol.iterator] != null) ||
    iter['@@iterator'] != null
  )
    return Array.from(iter);
}
function _arrayWithoutHoles(arr) {
  if (Array.isArray(arr)) return _arrayLikeToArray(arr);
}
function _arrayLikeToArray(arr, len) {
  if (len == null || len > arr.length) len = arr.length;
  for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
  return arr2;
}
function _objectWithoutProperties(source, excluded) {
  if (source == null) return {};
  var target = _objectWithoutPropertiesLoose(source, excluded);
  var key, i;
  if (Object.getOwnPropertySymbols) {
    var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
    for (i = 0; i < sourceSymbolKeys.length; i++) {
      key = sourceSymbolKeys[i];
      if (excluded.indexOf(key) >= 0) continue;
      if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
      target[key] = source[key];
    }
  }
  return target;
}
function _objectWithoutPropertiesLoose(source, excluded) {
  if (source == null) return {};
  var target = {};
  var sourceKeys = Object.keys(source);
  var key, i;
  for (i = 0; i < sourceKeys.length; i++) {
    key = sourceKeys[i];
    if (excluded.indexOf(key) >= 0) continue;
    target[key] = source[key];
  }
  return target;
}
import { Radio as AntdRadio, Input } from 'antd';
import 'antd/es/radio/style/index.js';
import classnames from 'classnames';
import React, { forwardRef, Fragment, useCallback, useRef } from 'react';
import { useFieldContext } from '../../_utils/contexts/FieldContext';
import useOther from '../../_utils/hooks/useOther';
import useRequestOptions from '../../_utils/hooks/useRequestOptions';
import createField from '../../createField';
import './index.less';
/**
 * 包含其他选项的单选框
 */
var RadioWithOther = /*#__PURE__*/ forwardRef(function (_ref, ref) {
  var value = _ref.value,
    onChange = _ref.onChange,
    optionsOrRequest = _ref.options,
    className = _ref.className,
    rest = _objectWithoutProperties(_ref, _excluded);
  var _useFieldContext = useFieldContext(),
    label = _useFieldContext.label;
  var _useRequestOptions = useRequestOptions(optionsOrRequest),
    options = _useRequestOptions.options;
  var otherRef = useRef('radio-other-' + Math.random());
  var _useOther = useOther({
      mode: 'single',
      value: value,
      onChange: onChange,
      options: options,
    }),
    otherChecked = _useOther.otherChecked,
    changeOtherChecked = _useOther.changeOtherChecked,
    selectedValue = _useOther.selectedValue,
    changeSelectedValue = _useOther.changeSelectedValue,
    otherValue = _useOther.otherValue,
    changeOtherValue = _useOther.changeOtherValue;
  var handleRadioChange = useCallback(
    function (val) {
      var found = options.find(function (option) {
        return option.value === val;
      });
      if (found) {
        changeOtherChecked(false);
        changeSelectedValue(found.value);
      } else {
        changeOtherChecked(true);
      }
    },
    [options, changeSelectedValue, changeOtherChecked],
  );
  var handleInputChange = useCallback(
    function (event) {
      changeOtherValue(event.target.value);
    },
    [changeOtherValue],
  );
  return /*#__PURE__*/ React.createElement(
    'span',
    {
      className: classnames('szzj-radio-with-other', className),
    },
    /*#__PURE__*/ React.createElement(
      AntdRadio.Group,
      _extends(
        {
          value: otherChecked
            ? otherRef.current
            : selectedValue === null || selectedValue === void 0
            ? void 0
            : selectedValue[0],
          onChange: function onChange(event) {
            handleRadioChange(event.target.value);
          },
        },
        rest,
        {
          className: 'szzj-radio-with-other-radio',
          options: [].concat(_toConsumableArray(options), [
            {
              value: otherRef.current,
              label: /*#__PURE__*/ React.createElement(
                Fragment,
                null,
                '\u5176\u4ED6',
                otherChecked &&
                  /*#__PURE__*/ React.createElement(Input, {
                    placeholder: '\u8BF7\u8F93\u5165'.concat(label || ''),
                    maxLength: 100,
                    className: 'szzj-radio-with-other-input',
                    value: otherValue,
                    onChange: handleInputChange,
                  }),
              ),
            },
          ]),
          ref: ref,
        },
      ),
    ),
  );
});
export default createField(RadioWithOther);
